package com.keshihao.eduservice.controller;


import com.keshihao.commonutils.R;
import com.keshihao.eduservice.entity.EduVideo;
import com.keshihao.eduservice.feign.VodClient;
import com.keshihao.eduservice.service.EduVideoService;
import com.keshihao.servicebase.exceptionhandler.TechException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author keshihao
 * @since 2020-05-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

  @Autowired
  private EduVideoService videoService;

  @Autowired
  VodClient vodClient;

  // 添加小节
  @PostMapping("/addVideo")
  public R addVideo(@RequestBody EduVideo eduVideo) {

    videoService.save(eduVideo);

    return R.ok();

  }

  // 删除小节
  @DeleteMapping("{id}")
  public R deleteVideo(@PathVariable String id) {

    EduVideo eduVideo = videoService.getById(id);

    String videoSourceId = eduVideo.getVideoSourceId();
    if (!StringUtils.isEmpty(videoSourceId)) {
      // 根据视频id，删除阿里云中的视频资源
      R r = vodClient.removeAlyVideo(videoSourceId);
      if (r.getCode() == 20001) {
        throw new TechException(20001,"删除视频出错");
      }
    }

    // 删除小节信息
    videoService.removeById(id);

    return R.ok();

  }



  // 修改小节 TODO
  @PostMapping("/updateVideo")
  public R updateVideo(@RequestBody EduVideo eduVideo) {
    videoService.updateById(eduVideo);
    return R.ok();
  }

  // 根据id查询小节信息
  @GetMapping("/getVideoById/{id}")
  public R getVideoById(@PathVariable String id) {
    EduVideo eduVideo = videoService.getById(id);
    return R.ok().data("eduVideo",eduVideo);
  }


}

